package com.spark.rectify

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.View
import com.spark.colliery.common.ext.gone
import com.spark.common.BaseFragment
import com.spark.common.helper.MutableStatePagerAdapter
import kotlinx.android.synthetic.main.frag_handle.*

class RectAgencyFragment : BaseFragment() {
    private lateinit var rectAgencyAdapter: MutableStatePagerAdapter

    companion object {
        fun newInstance(data: String): RectAgencyFragment {
            val target = RectAgencyFragment()
            val args = Bundle()
            args.putString("data", data)
            target.arguments = args
            return target
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_handle
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        var state = 0
        /**
         * 0 -> 纠违审核
         *   纠违信息 RectInfo
         *   处罚单预览 PunishPreview
         * 1 -> 纠违确认
         *   待确人 PuninshPreview
         * 2 -> 待审阅
         *   纠违确认
         *
         */
        var tabTitles = mutableListOf<String>()
        var frags = mutableListOf<Fragment>()
        when (state) {
            0 -> {
                tabTitles.apply {
                    add("纠违信息")
                    add("处罚单预览")
                }
                frags.apply {
                    add(RectInfoFragment.newInstance())
                    add(PunishOrderPreviewFragment.newInstance())
                }
            }
            1 -> {
                tabTitles.add("待确认")
                frags.add(PunishOrderPreviewFragment.newInstance())
            }
            2 -> {

            }
        }
        rectAgencyAdapter = object : MutableStatePagerAdapter(childFragmentManager) {
            override fun getCount(): Int {
                return tabTitles.size
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return tabTitles[position]
            }
        }
        rectAgencyAdapter.adds(frags)
        tabs.gone(tabTitles.size == 1)
        tabTitles.forEach {
            tabs.addTab(tabs.newTab().setText(it))
        }
        pager.apply {
            adapter = rectAgencyAdapter
            canScroll = false
        }
        tabs.setupWithViewPager(pager)
    }
}